import time

import requests
from bs4 import BeautifulSoup


def get_ip(url, f):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
    }
    response = requests.get(url, headers=headers)
    html = response.content.decode('utf-8')
    soup = BeautifulSoup(html, 'lxml')
    get_ele = soup.find('table', attrs={'class': 'table table-bordered table-striped'}).find('tbody').find_all('tr')
    for data in get_ele:
        tds = data.find_all('td')
        ip = tds[0].string
        port = tds[1].string
        anonymous = tds[2].string
        ip_type = tds[3].string
        ip_address = tds[4].string
        ip_speed = tds[5].string
        ip_last_time = tds[6].string
        f.write('{},{},{},{},{},{},{}\n'.format(ip, port, anonymous, ip_type, ip_address, ip_speed, ip_last_time))


def save():
    with open('kauidaili.csv', 'a', encoding='utf-8') as f:
        for x in range(1, 10):
            time.sleep(1)
            url = 'https://www.kuaidaili.com/free/inha/{}/'.format(x)
            get_ip(url, f)

if __name__ == '__main__':
    save()
